package cn.nfwx.tv.base;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.squareup.leakcanary.RefWatcher;
import com.trello.rxlifecycle2.components.support.RxDialogFragment;

import cn.nfwx.tv.BuildConfig;

/**
 * Created by zhouzechao on 2017/11/17.
 */

public abstract class BaseDialogFragment extends RxDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getDialog().getWindow();
        int windowAnimations = getWindowAnimations();
        if (windowAnimations != 0) {
            window.setWindowAnimations(windowAnimations);
        }
        if (isTranslucentStatus()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }

        //设置背景透明度
        float amount = isTranslucentBackground() ? 0 : getDimAmount();
        if (amount >= 0 && amount <= 1) {
            window.setDimAmount(amount);
        }

        window.setGravity(getGravity()); //设置dialog的位置

        if (isMatchParent()) {
            window.getDecorView().setPadding(0, 0, 0, 0); //消除边距

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = getWidth();   //设置宽度充满屏幕
            lp.height = getHeight();
            window.setAttributes(lp);
        }
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        getDialog().setCanceledOnTouchOutside(isOutsideTouchable());

        View view;
        int layoutId = getLayoutId();
        if (layoutId > 0) {
            view = inflater.inflate(layoutId, container, false);
        } else {
            view = getCustomView(inflater.getContext());
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
    }


    protected <T extends View> T findView(int id) {
        return (T) getView().findViewById(id);
    }

    protected int getGravity() {
        return Gravity.CENTER;
    }

    protected int getHeight() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    protected int getWidth() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    //是否透明背景
    protected boolean isTranslucentBackground() {
        return false;
    }

    //背景透明度
    protected float getDimAmount() {
        return -1;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return false;
    }

    protected boolean isOutsideTouchable() {
        return false;
    }

    /**
     * 是否透明状态栏
     */
    protected boolean isTranslucentStatus() {
        return true;
    }

    //自定义动画
    protected int getWindowAnimations() {
        return 0;
    }

    /**
     * 设置自定义view
     */
    protected View getCustomView(Context context) {
        return null;
    }

    /**
     * 设置布局
     */
    protected abstract int getLayoutId();

    /**
     * 初始化布局
     */
    protected abstract void initView(Bundle savedInstanceState);
}
